#include "settingsscreen.h"
#include "ui_settingsscreen.h"

SettingsScreen::SettingsScreen(QWidget *parent, QString newFilepath) :
    QWidget(parent),
    ui(new Ui::SettingsScreen)
{
    ui->setupUi(this);

    this->setGeometry(0, 0, 640, 640);
    this->filepath = newFilepath;

    soundOn = dynamic_cast<BaseWindow*>(this->parent()->parent())->getSound();
    invertOn = dynamic_cast<BaseWindow*>(this->parent()->parent())->getInvertUpDown();

    //Create Background Image
    QString background = newFilepath + "Images/settingsbg.png";
    qDebug() << "instruction screen background: " << background << endl;
    bg = QImage(background);

    //Load Font
    QFontDatabase::addApplicationFont(newFilepath + "Qubix.ttf");
    buttonFont = new QFont("Qubix", 28, -1, false);

    //Set Label Text
    ui->soundLabel->setFont(*buttonFont);
    ui->invertLabel->setFont(*buttonFont);
    ui->soundLabel->setText("<font color='white'>Sound</font>");
    ui->invertLabel->setText("<font color='white'>Invert Up/Down</font>");

    //Create Buttons
    back = new TextButton(this, "back", "white", "#29E0FF");
    back->setFont(*buttonFont);
    back->setGeometry(16, 6, 102, 34);  //With font size of 28, width is ((letters + spaces/2) * 25 + 2)
    connect(back, SIGNAL(clicked(QMouseEvent*)), this, SLOT(on_back_clicked()));
    back->show();

    if(soundOn)
    {
        sound = new TextButton(this, "On", "white", "#29E0FF");
        sound->setGeometry(500, 230, 52, 34);
    } else {
        sound = new TextButton(this, "Off", "white", "#29E0FF");
        sound->setGeometry(500, 230, 77, 34);
    }
    sound->setFont(*buttonFont);
    connect(sound, SIGNAL(clicked(QMouseEvent*)), this, SLOT(on_sound_clicked()));
    sound->show();

    if(invertOn)
    {
        invertUpDown = new TextButton(this, "On", "white", "#29E0FF");
        invertUpDown->setGeometry(500, 290, 52, 34);
    } else {
        invertUpDown = new TextButton(this, "Off", "white", "#29E0FF");
        invertUpDown->setGeometry(500, 290, 77, 34);
    }
    invertUpDown->setFont(*buttonFont);
    connect(invertUpDown, SIGNAL(clicked(QMouseEvent*)), this, SLOT(on_invertUpDown_clicked()));
    invertUpDown->show();
}

SettingsScreen::~SettingsScreen()
{
    delete ui;
}

void SettingsScreen::on_back_clicked()
{
//    dynamic_cast<QWidget*>(this->parent())->show();
    this->deleteLater();
}

void SettingsScreen::on_sound_clicked()
{
//    //mute/unmute
//    sound->setGeometry(sound->x(), sound->y(), 214, sound->height());
//    sound->setButtonText("Sound Off");

    dynamic_cast<BaseWindow*>(this->parent()->parent())->toggleSound();
    soundOn ^= true;
    if(soundOn)
    {
        sound->setButtonText("On");
        sound->setGeometry(500, 230, 52, 34);
    } else {
        sound->setButtonText("Off");
        sound->setGeometry(500, 230, 77, 34);
    }
}

void SettingsScreen::on_invertUpDown_clicked()
{
    //change keys behavior
    dynamic_cast<BaseWindow*>(this->parent()->parent())->toggleInvertUpDown();
    invertOn ^= true;
    if(invertOn)
    {
        invertUpDown->setButtonText("On");
        invertUpDown->setGeometry(500, 290, 52, 34);
    } else {
        invertUpDown->setButtonText("Off");
        invertUpDown->setGeometry(500, 290, 77, 34);
    }
}

void SettingsScreen::paintEvent(QPaintEvent *event)
{
    //Create the painter
    QPainter painter(this);

    //Draw background image
    QRect imageRect(0,0,this->width(), this->height());
    painter.drawImage(imageRect, bg);

    painter.end();
}
